<template>
  <div>

    <el-dialog :title="title" :visible="visible" :modal="false" width="60%" top="200px" custom-class="customerDialogClass"
      :before-close="dialogVisible" @opened="opens">

      <div class="formList" style="margin-bottom:10px">
        <el-form :inline="true">
          <el-form-item>
            <el-cascader size="large" :options="ops" v-model="selectedOp" @change="handleChange">
            </el-cascader>
          </el-form-item>
          <el-form-item>
            <el-button size="medium"  @click="button">查询</el-button>
          </el-form-item>
        </el-form>
      </div>

      <div>
        <div id="row1">
          <dv-border-box-9 style="width: 49%; float: left; overflow: hidden ">
            <div ref="chart1" style="height:320px"></div>
          </dv-border-box-9>

          <dv-border-box-9 style="width: 49%; float: right; overflow: hidden">
            <div ref="chart2" style="height:320px"></div>
          </dv-border-box-9>
        </div>

        <div id="row2" style="margin-top:20px">
          <dv-border-box-9 style="width: 49%; float: left; overflow: hidden; margin-top:20px">
            <div ref="chart3" style="height:320px"></div>
          </dv-border-box-9>

          <dv-border-box-9 style="width: 49%; float: right; overflow: hidden; margin-top:20px">
            <div ref="chart4" style="height:320px"></div>
          </dv-border-box-9>
        </div>

      </div>

    </el-dialog>

  </div>
</template>

<script>
import * as echarts from "echarts";
import { provinceAndCityData } from 'element-china-area-data'
var mychart1;
var mychart2;
var mychart3;
var mychart4;
// 生成扇形的曲面参数方程，用于 series-surface.parametricEquation
function getParametricEquation(startRatio,endRatio,isSelected,isHovered,k) {
  // 计算
  let midRatio=(startRatio+endRatio)/2;

  let startRadian=startRatio*Math.PI*2;
  let endRadian=endRatio*Math.PI*2;
  let midRadian=midRatio*Math.PI*2;

  // 如果只有一个扇形，则不实现选中效果。
  if(startRatio===0&&endRatio===1) {
    isSelected=false;
  }

  // 通过扇形内径/外径的值，换算出辅助参数 k（默认值 1/3）
  k=typeof k!=="undefined"? k:1/3;

  // 计算选中效果分别在 x 轴、y 轴方向上的位移（未选中，则位移均为 0）
  let offsetX=isSelected? Math.cos(midRadian)*0.1:0;
  let offsetY=isSelected? Math.sin(midRadian)*0.1:0;

  // 计算高亮效果的放大比例（未高亮，则比例为 1）
  let hoverRate=isHovered? 1.05:1;

  // 返回曲面参数方程
  return {
    u: {
      min: -Math.PI,
      max: Math.PI*3,
      step: Math.PI/32
    },

    v: {
      min: 0,
      max: Math.PI*2,
      step: Math.PI/20
    },

    x: function(u,v) {
      if(u<startRadian) {
        return (
          offsetX+
          Math.cos(startRadian)*(1+Math.cos(v)*k)*hoverRate
        );
      }
      if(u>endRadian) {
        return (
          offsetX+Math.cos(endRadian)*(1+Math.cos(v)*k)*hoverRate
        );
      }
      return offsetX+Math.cos(u)*(1+Math.cos(v)*k)*hoverRate;
    },

    y: function(u,v) {
      if(u<startRadian) {
        return (
          offsetY+
          Math.sin(startRadian)*(1+Math.cos(v)*k)*hoverRate
        );
      }
      if(u>endRadian) {
        return (
          offsetY+Math.sin(endRadian)*(1+Math.cos(v)*k)*hoverRate
        );
      }
      return offsetY+Math.sin(u)*(1+Math.cos(v)*k)*hoverRate;
    },

    z: function(u,v) {
      if(u<-Math.PI*0.5) {
        return Math.sin(u);
      }
      if(u>Math.PI*2.5) {
        return Math.sin(u);
      }
      return Math.sin(v)>0? 1:-1;
    }
  };
}

// 生成模拟 3D 饼图的配置项
function getPie3D(pieData,internalDiameterRatio) {
  let series=[];
  let sumValue=0;
  let startValue=0;
  let endValue=0;
  let legendData=[];
  let k=
    typeof internalDiameterRatio!=="undefined"?
      (1-internalDiameterRatio)/(1+internalDiameterRatio):
      1/3;

  // 为每一个饼图数据，生成一个 series-surface 配置
  for(let i=0;i<pieData.length;i++) {
    sumValue+=pieData[i].value;

    let seriesItem={
      name: typeof pieData[i].name==="undefined"?
        `series${i}`:pieData[i].name,
      type: "surface",
      parametric: true,
      wireframe: {
        show: false
      },
      pieData: pieData[i],
      pieStatus: {
        selected: false,
        hovered: false,
        k: k
      }
    };

    if(typeof pieData[i].itemStyle!="undefined") {
      let itemStyle={};

      typeof pieData[i].itemStyle.color!="undefined"?
        (itemStyle.color=pieData[i].itemStyle.color):
        null;
      typeof pieData[i].itemStyle.opacity!="undefined"?
        (itemStyle.opacity=pieData[i].itemStyle.opacity):
        null;

      seriesItem.itemStyle=itemStyle;
    }
    series.push(seriesItem);
  }

  // 使用上一次遍历时，计算出的数据和 sumValue，调用 getParametricEquation 函数，
  // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation，也就是实现每一个扇形。
  for(let i=0;i<series.length;i++) {
    endValue=startValue+series[i].pieData.value;

    series[i].pieData.startRatio=startValue/sumValue;
    series[i].pieData.endRatio=endValue/sumValue;
    series[i].parametricEquation=getParametricEquation(
      series[i].pieData.startRatio,
      series[i].pieData.endRatio,
      true,
      false,
      1
    );

    startValue=endValue;

    legendData.push(series[i].name);
  }

  // 补充一个透明的圆环，用于支撑高亮功能的近似实现。
  series.push({
    name: "mouseoutSeries",
    type: "surface",
    parametric: true,
    wireframe: {
      show: false
    },
    itemStyle: {
      opacity: 1,
      color: "rgba(18,236,252,.5)"
    },
    parametricEquation: {
      u: {
        min: 0,
        max: Math.PI*2,
        step: Math.PI/20
      },
      v: {
        min: 0,
        max: Math.PI,
        step: Math.PI/1.4
      },
      x: function(u,v) {
        return Math.sin(v)*Math.sin(u)+Math.sin(u);
      },
      y: function(u,v) {
        return Math.sin(v)*Math.cos(u)+Math.cos(u);
      },
      z: function(u,v) {
        return Math.cos(v)>0? 0.1:-0.1;
      }
    }
  });

  // 准备待返回的配置项，把准备好的 legendData、series 传入。
  let option={
    //animation: false,
    legend: {
      show: true,
      data: legendData,
      textStyle: {
        color: '#ffffff',
        fontSize: 12,
        fontWeight: 0
      },
      top: 15
    },
    tooltip: {
      formatter: params => {
        if(params.seriesName!=='mouseoutSeries') {
          return `${params.seriesName}<br/><span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${params.color};"></span>${option.series[params.seriesIndex].pieData.value}`;
        }
      }
    },
    xAxis3D: {
      min: -1.3,
      max: 1.3
    },
    yAxis3D: {
      min: -1.3,
      max: 1.3
    },
    zAxis3D: {
      min: -1.3,
      max: 1.3
    },
    grid3D: {
      show: false,
      boxHeight: 10,
      // top: '30%',
      left: "0",
      bottom: "50%",
      viewControl: {
        //3d效果可以放大、旋转等，请自己去查看官方配置
        alpha: 20,
        // beta: 40,
        rotateSensitivity: 0,
        zoomSensitivity: 0,
        panSensitivity: 0,
        autoRotate: true,
      },
    },
    series: series
  };
  return option;
}
export default {

  data() {
    return {
      //级联选择器
      ops: provinceAndCityData,
      selectedOp: [],

      //仪表盘
      option3: {
        series: [
          {
            type: 'gauge',
            z: 3,
            min: 0,
            max: 15,
            startAngle: 200,
            endAngle: -20,
            radius: '70%',
            axisLine: {            // 坐标轴线
              lineStyle: {       // 属性lineStyle控制线条样式
                width: 25,
                shadowColor: '#0092EE',
                shadowBlur: 50,
                color: [[0.2,'#0092EE'],[1,'#BECAD8']],
              }
            },
            axisLabel: {
              show: false,
              fontSize: 30,

            },
            axisTick: {            // 坐标轴小标记
              show: true,
              length: 10,
              lineStyle: {
                width: 3,
              }
            },
            pointer: {
              width: 20,
              length: '70%'
            },
            splitLine: {           // 分隔线
              show: true,
              length: 30,
              lineStyle: {
                width: 3,
              }
            },
            title: {
              offsetCenter: [0,'-20%'],       // x, y，单位px
              fontSize: 30,
              color: '#fff',
            },
            detail: {
              fontSize: 30,
              color: '#fff',
              formatter: function(value) {
                return '{a|'+value+'}';
              },
              rich: {
                a: {
                  fontSize: 30,
                  color: '#0093EE'
                }
              }
            },
            data: [{ value: Math.floor((Math.random()*25)+18),name: '污染指数' }]
          },
          {
            type: 'gauge',
            center: ['18%','58%'],    // 默认全局居中
            radius: '55%',
            min: 0,
            max: 12,
            startAngle: 200,
            endAngle: 50,
            splitNumber: 8,
            axisLabel: {
              show: false,
              fontSize: 30,
            },
            axisLine: {            // 坐标轴线
              lineStyle: {       // 属性lineStyle控制线条样式
                width: 18,
                shadowColor: '#0092EE',
                shadowBlur: 50,
                color: [[0.2,'#0092EE'],[0.8,'#0092EE'],[1,'#BECAD8']],
              }
            },
            axisTick: {            // 坐标轴小标记
              show: true,
              length: 7,
              lineStyle: {
                width: 3,
              }
            },
            pointer: {
              width: 13,
              length: '70%'
            },
            splitLine: {           // 分隔线
              show: true,
              length: 23,
              lineStyle: {
                width: 3,
              }
            },
            title: {
              offsetCenter: [0,'-20%'],       // x, y，单位px
              fontSize: 30,
              color: '#fff',
            },
            detail: {
              fontSize: 30,
              color: '#fff',
              formatter: function(value) {
                return '{a|'+value+'}℃';
              },
              rich: {
                a: {
                  fontSize: 30,
                  color: '#0093EE'
                }
              }
            },
            data: [{ value: Math.floor((Math.random()*9)+28),name: '温度' }]
          },
          {
            type: 'gauge',
            center: ['82%','58%'],    // 默认全局居中
            radius: '55%',
            min: 0,
            max: 2,
            startAngle: 130,
            endAngle: -20,
            splitNumber: 4,
            axisLabel: {
              show: false,
              fontSize: 30,
            },
            axisLine: {            // 坐标轴线
              lineStyle: {       // 属性lineStyle控制线条样式
                width: 18,
                shadowColor: '#0092EE',
                shadowBlur: 50,
                color: [[0.2,'#BECAD8'],[0.8,'#0092EE'],[1,'#25C0C8']],
              }
            },
            axisTick: {            // 坐标轴小标记
              show: true,
              length: 7,
              lineStyle: {
                width: 3,
              }
            },
            pointer: {
              width: 13,
              length: '70%'
            },
            splitLine: {           // 分隔线
              show: true,
              length: 23,
              lineStyle: {
                width: 3,
              }
            },
            title: {
              offsetCenter: [0,'-20%'],       // x, y，单位px
              fontSize: 30,
              color: '#fff',
            },
            detail: {
              fontSize: 30,
              color: '#fff',
              formatter: function(value) {
                return '{a|'+value+'}mm';
              },
              rich: {
                a: {
                  fontSize: 30,
                  color: '#0093EE'
                }
              }
            },
            data: [{ value: Math.floor((Math.random()*30)+0),name: '降水量' }]
          },

        ]
      },

      option5: getPie3D([
        {
          name: '购物中心',
          value: 15,
          itemStyle: {
            color: '#8d7fec',
          }
        },{
          name: '百货商店',
          value: 12,
          itemStyle: {
            color: '#5085f2'
          }
        },
        {
          name: '住宅配套商业',
          value: 4,
          itemStyle: {
            color: '#e75fc3'
          }
        },
        {
          name: '写字楼配套商业',
          value: 3,
          itemStyle: {
            color: '#f87be2'
          }
        },
        {
          name: '酒店配套商业',
          value: 8,
          itemStyle: {
            color: '#57e7ec'
          }
        },
        {
          name: '数码家电卖场',
          value: 3,
          itemStyle: {
            color: '#e75fc3'
          }
        },
        {
          name: '服装卖场',
          value: 6,
          itemStyle: {
            color: '#cf9ef1'
          }
        },
        {
          name: '其他',
          value: 10,
          itemStyle: {
            color: '#fd5d48'
          }
        },
      ],0.59),

      option6: {
        title: {
          top: '5%',
          left: 'center',
          textStyle: {
            color: '#FFF',
            align: 'center',
          }
        },
        // backgroundColor:'#11183c',
        grid: {
          left: '9%',
          right: '10%',
          top: '20%',
          bottom: '15%',
          containLabel: true
        },
        tooltip: {
          show: true,
          trigger: 'axis',
          formatter: function(params) {
            let str=params[0].name+'<br/>';
            params.forEach(item => {
              if(item.seriesName=='销售额') {
                str+=item.marker+item.seriesName+': '+item.data+'<br/>';
              } else if(item.seriesName=="增长率") {
                str+=item.marker+item.seriesName+': '+item.data+'%';
              }
            });
            return str;
          }
        },
        legend: {
          show: true,
          x: 'center',
          top: '12%',
          y: '35',
          icon: 'stack',
          itemWidth: 16,
          itemHeight: 10,
          textStyle: {
            color: '#E0E0E0'
          },
          data: ['销售额','增长率']
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            axisLabel: {
              color: '#AFBCC4',
              fontSize: 12,
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: '#397cbc'
              }
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: '#195384'
              }
            },
            data: ['4月','5月','6月','7月','8月','9月','10月']
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '销售额',
            axisLabel: {
              // formatter: '{value}',
              textStyle: {
                color: '#AFBCC4',
                fontSize: 12,
              }
            },
            axisLine: {
              lineStyle: {
                color: '#fff'
              }
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: '#D4D4D8'
              }
            }
          },
          {
            type: 'value',
            name: '增长率',
            axisLabel: {
              formatter: '{value} %',
              textStyle: {
                color: '#AFBCC4',
                fontSize: 12,
              }
            },
            axisLine: {
              lineStyle: {
                color: '#fff'
              }
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: '#11366e'
              }
            }
          }
        ],
        series: [
          {
            name: '销售额',
            type: 'line',
            symbol: 'circle',
            symbolSize: 8,
            yAxisIndex: 0,
            itemStyle: {
              normal: {
                color: '#00FFFF',
                lineStyle: {
                  color: "#00FFFF",
                  width: 1
                },
                areaStyle: {
                  //color: '#94C9EC'
                  color: new echarts.graphic.LinearGradient(0,1,0,0,[{
                    offset: 0,
                    color: 'rgba(0, 255, 255, 0.1)'
                  },{
                    offset: 1,
                    color: 'rgba(0, 255, 255, 0.52)'
                  }]),
                }
              }
            },
            markPoint: {
              itemStyle: {
                normal: {
                  color: 'red'
                }
              }
            },
            data: [20,32,21,34,19,20,21]
          },
          {
            name: '增长率',
            type: 'line',
            symbol: 'circle',
            symbolSize: 8,
            yAxisIndex: 1,
            itemStyle: {
              normal: {
                color: '#FF9000',
                lineStyle: {
                  color: "#FF9000",
                  width: 1
                },
                areaStyle: {
                  //color: '#94C9EC'
                  color: new echarts.graphic.LinearGradient(0,1,0,0,[{
                    offset: 0,
                    color: 'rgba(255, 144, 0, 0.1)'
                  },{
                    offset: 1,
                    color: 'rgba(255, 144, 0, 0.52)'
                  }]),
                }
              }
            },
            data: [1,0,2,1,1,2,3]
          },
        ]
      },
      option7: {
        "color": ["#3cefff"],
        "tooltip": {},
        "grid": {
          "containLabel": true
        },
        "xAxis": [{
          "type": "category",
          "data": ["01月","02月","03月","04月","05月","06月","07月","08月","09月","10月","11月","12月"],
          "axisTick": {
            "alignWithLabel": true
          },
          "nameTextStyle": {
            "color": "#82b0ec"
          },
          "axisLine": {
            "lineStyle": {
              "color": "#82b0ec"
            }
          },
          "axisLabel": {
            "textStyle": {
              "color": "#82b0ec"
            }
          }
        }],
        "yAxis": [{
          "type": "value",
          "axisLabel": {
            "textStyle": {
              "color": "#82b0ec"
            },
            "formatter": "{value}%"
          },
          "splitLine": {
            "lineStyle": {
              "color": "#0c2c5a"
            }
          },
          "axisLine": {
            "show": false
          }
        }],
        "series": [{
          "name": "",
          "type": "pictorialBar",
          "symbolSize": [20,10],
          "symbolOffset": [0,-5],
          "symbolPosition": "end",
          "z": 12,
          "label": {
            "normal": {
              "show": true,
              "position": "top",
              "formatter": "{c}%"
            }
          },
          "data": [6,7,8,9,6,7,8,9]
        },{
          "name": "",
          "type": "pictorialBar",
          "symbolSize": [20,10],
          "symbolOffset": [0,5],
          "z": 12,
          "data": [6,7,8,9,6,7,8,9]
        },{
          "type": "bar",
          "itemStyle": {
            "normal": {
              "opacity": 0.7
            }
          },
          "barWidth": "20",
          "data": [6,7,8,9,6,7,8,9],
          "markLine": {
            "silent": true,
            "symbol": "none",
            "label": {
              "position": "middle",
              "formatter": "{b}"
            },
            "data": [{
              "name": "目标值",
              "yAxis": 5,
              "lineStyle": {
                "color": "#ffc832"
              },
              "label": {
                "position": "end",
                "formatter": "{b}\n {c}%"
              }
            }]
          }
        },
        {
          type: 'effectScatter',
          silent: true,
          tooltip: {
            show: false
          },
          zlevel: 3,
          symbolSize: 10,
          showEffectOn: 'render',
          rippleEffect: {
            brushType: 'stroke',
            color: '#3cefff',
            scale: 5
          },
          itemStyle: {
            color: '#3cefff'
          },
          hoverAnimation: true,
          data: [0,0,0,0,0,0,0,0,0,0,0,0,0]
        },
        ]
      }


    }
  },



  props: {
    // 标题
    title: {
      type: String,
      default: '提示'
    },
    // 弹框控制
    visible: {
      type: Boolean,
      default: false
    },
    // 文本
    text: {
      type: String,
      default: '文本信息'
    },
    // 接收的数据
    // data: {
    //     type: Object,
    //     default: () => {}
    // },
    // 是否居中
    center: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    dialogVisible() {
      this.$emit('update:visible',false);
    },
    opens() {
      this.$nextTick(() => {
        this.pie1()
        // initChart()
      })
    },
    pie1() {
      mychart1=echarts.init(this.$refs.chart1)
      mychart1.setOption(this.option5)
      mychart2=echarts.init(this.$refs.chart2)
      mychart2.setOption(this.option7)
      mychart3=echarts.init(this.$refs.chart3)
      mychart3.setOption(this.option6)
      mychart4=echarts.init(this.$refs.chart4)
      mychart4.setOption(this.option3)
    },
    //利用button渲染echarts初始不渲染
    button() {
      mychart1.clear()
      mychart1.setOption(this.option5)
      mychart2.clear()
      this.option7.series[0].data=[]
      this.option7.series[1].data=[]
      this.option7.series[2].data=[]
      let randomArr=[];
      for(let i=0;i<8;i++) {
        randomArr.push(Math.floor(Math.random()*8)+1);
      }
      this.option7.series[0].data=randomArr
      this.option7.series[1].data=randomArr
      this.option7.series[2].data=randomArr
      mychart2.setOption(this.option7)
      mychart3.clear()
      let op6Arr1=[];
      let op6Arr2=[];
      for(let i=0;i<7;i++) {
        op6Arr1.push(Math.floor(Math.random()*20)+15);
        op6Arr2.push(Math.floor(Math.random()*3)+1);
      }
      this.option6.series[0].data=op6Arr1
      this.option6.series[1].data=op6Arr2
      mychart3.setOption(this.option6)
      mychart4.clear()
      this.option3.series[2].data=[{ value: Math.floor((Math.random()*30)+0),name: '降水量' }]
      this.option3.series[1].data=[{ value: Math.floor((Math.random()*9)+28),name: '温度' }]
      this.option3.series[0].data=[{ value: Math.floor((Math.random()*9)+28),name: '污染指数' }]
      mychart4.setOption(this.option3)

    },

    //级联选择器
    handleChange(value) {
    },


  }
};
</script>

<style lang="scss" >
.customerDialogClass {
  width: 900px;
  height: 800px;
}
::v-deep .el-dialog {
  background-color: rgba(34, 52, 87, 0.8);
}
</style>


